﻿Module RoleDao

    Public Sub insert(ByVal name As String, ByVal description As String)
        DataAccessConnection.openConnection()
        Dim command As New Data.OleDb.OleDbCommand

        Dim query As String = ""

        query = "INSERT INTO ROLE(name, description) VALUES(@name, @description)"
        command.Connection = connection
        command.CommandType = CommandType.Text
        command.CommandText = query
        command.Parameters.AddWithValue("nombre", name)
        command.Parameters.AddWithValue("descripcion", description)
        command.ExecuteNonQuery()

        DataAccessConnection.closeConnection()
    End Sub

    Public Function searchDataByFilter(ByVal id As Integer, ByVal name As String, ByVal description As String) As DataTable
        DataAccessConnection.openConnection()
        Dim command As New Data.OleDb.OleDbCommand
        Dim table As DataTable = New DataTable

        Dim query As String = ""

        query = "Select * FROM ROLE "
        query &= " where (id = @id OR @id = 0) "
        query &= " AND (name like ""%@name%"" OR @name="""") "
        query &= " AND description like ""%@description%"" "
        command.Connection = connection
        command.CommandType = CommandType.Text
        command.CommandText = query

        If id = Nothing Then
            command.Parameters.AddWithValue("id", 0)
        Else
            command.Parameters.AddWithValue("id", id)
        End If

        If name = Nothing Then
            command.Parameters.AddWithValue("name", "")
        Else
            command.Parameters.AddWithValue("name", name)
        End If

        'If descripcion = Nothing Then
        ' command.Parameters.AddWithValue("descripcion", DBNull.Value)
        'Else
        'command.Parameters.AddWithValue("descripcion", descripcion)
        'End If

        table.Load(command.ExecuteReader)
        DataAccessConnection.closeConnection()
        Return table
    End Function
End Module